<!--
 * @Description: 用户密码重置
 * @Autor: Zhu_liangyu
 * @Date: 2020-06-28 20:18:52
--> 
<template>
  <a-form-model
    ref="restUserPassForm"
    :rules="rules"
    :model="userForm"
    :label-col="labelCol"
    :wrapper-col="wrapperCol"
  >
    <a-form-model-item label="旧密码" prop="userPassword">
      <a-input v-model="userForm.userPassword" />
    </a-form-model-item>
    <a-form-model-item label="新密码" prop="newPassword">
      <a-input v-model="userForm.newPassword" />
    </a-form-model-item>
    <a-form-model-item label="确认密码" prop="checkPassword">
      <a-input v-model="userForm.checkPassword" />
    </a-form-model-item>
  </a-form-model>
</template>
<script>
import { validUserPassword } from "@/utils/validate";
import { restPass } from "@/api/org/user";
export default {
  data() {
    let validatePass = (rule, value, callback) => {
      if (!validUserPassword(value)) {
        callback(new Error("密码必须在6-16位之间"));
      } else {
        callback();
      }
    };
    let validatePass2 = (rule, value, callback) => {
      if (!validUserPassword(value)) {
        callback(new Error("密码必须在6-16位之间"));
      } else if (value == this.userForm.userPassword) {
        callback(new Error("新旧密码不能相同"));
      }
      callback();
    };
    let validatePass3 = (rule, value, callback) => {
      if (!validUserPassword(value)) {
        callback(new Error("密码必须在6-16位之间"));
      } else if (value !== this.userForm.newPassword) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };
    return {
      labelCol: { span: 4 },
      wrapperCol: { span: 20 },
      userForm: {
        userId: this.$store.getters.userId,
        userPassword: null,
        newPassword: null,
        checkPassword: null
      },
      rules: {
        userPassword: [
          { validator: validatePass, trigger: "blur", required: true }
        ],
        newPassword: [
          { validator: validatePass2, trigger: "blur", required: true }
        ],
        checkPassword: [
          { validator: validatePass3, trigger: "blur", required: true }
        ]
      }
    };
  },
  methods: {
    onSubmit() {
      this.$refs["restUserPassForm"].validate(valid => {
        let params = {
          oldPassword: this.userForm.userPassword,
          newPassword: this.userForm.newPassword
        };
        restPass(params).then(response => {
          if (response.data.code == 60000) {
            this.$message.success(
              "密码修改成功,正在为您跳转,请重新登录",
              3,
              () => {
                this.logout();
              }
            );
          }
        });
      });
    },
    async logout() {
      await this.$store.dispatch("user/logout");
      this.$router.push(`/login?redirect=${this.$route.fullPath}`);
    }
  }
};
</script>

